package com.atguigu.guli.service.ucenter.controller.api;


import com.atguigu.guli.service.base.dto.MemberDto;
import com.atguigu.guli.service.base.result.R;
import com.atguigu.guli.service.base.utils.JwtHelper;
import com.atguigu.guli.service.base.utils.JwtInfo;
import com.atguigu.guli.service.ucenter.entity.Member;
import com.atguigu.guli.service.ucenter.entity.vo.ApiMemberVo;
import com.atguigu.guli.service.ucenter.service.MemberService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author yz
 * @since 2022-08-01
 */
@RestController
@RequestMapping("/api/ucenter/member")

public class ApiMemberController {
    @Autowired
    MemberService memberService;

    /**
     * 用户注册
     * @param memberVo
     * @return
     */
    @PostMapping("/register")
    public R register(@RequestBody ApiMemberVo memberVo){
        memberService.register(memberVo);
        return R.ok();
    }

    //用户的认证
    @PostMapping("/login")
    public R login(@RequestParam("mobile") String mobile,
                   @RequestParam("password")String password){
       String token= memberService.login(mobile,password);
       return R.ok().data("token",token);
    }

    //用户信息的回显
    //3.解析token获取用户信息
    @GetMapping("/auth/getUserInfo")
    public R getUserInfo(HttpServletRequest request){//从请求报文中获取请求头中的信息来解析用户信息
        JwtInfo jwtInfo = JwtHelper.getJwtInfo(request);
        return  R.ok().data("item",jwtInfo);
    }
    //查询订单中的用户信息
    @GetMapping("/getMemberDto/{id}")
    public R getMemberDto(@PathVariable("id") String id){
        //Member member = memberService.getById(id);
        //MemberDto memberDto = new MemberDto();
        //BeanUtils.copyProperties(member,memberDto);
        //return R.ok().data("item",memberDto);
        Member member = memberService.getById(id);
        MemberDto memberDto = new MemberDto();
        BeanUtils.copyProperties(member , memberDto);
        return R.ok().data("item", memberDto);

    }
}

